package shop.swagger;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@EnableOpenApi
// @Profile("!pro")
//@ConditionalOnProperty(name = "swagger.enable",havingValue = "true")
public class SwaggerConfig {

    @Value("${swagger.enable:true}")
    private boolean enable;

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)      // 选择swagger2版本
                .apiInfo(apiInfo())         //定义api文档汇总信息
                .groupName("order")
                .select()
                .apis(RequestHandlerSelectors
                        .basePackage("shop.order.controller"))  // 指定生成api文档的包
                .paths(PathSelectors.any())     // 指定所有路径
                .build()
                // 如果为生产环境，则不创建swagger
                .enable(enable);
    }

    /**
     * 构建文档api信息
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("订单服务接口文档")     // 文档标题
                .contact(new Contact("开发者", "http://baidu.com", "382779056@163.com"))   //联系人信息
                .description("订单服务")      //描述
                .version("1.0.1")     //文档版本号
                .termsOfServiceUrl("/order")     //网站地址
                .build();
    }
}
